﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CeA.Reports.Atendimento.Web.ServerControls;
using System.Collections.Generic;
using CeA.Reports.Atendimento.Web.Helpers;
using CeA.Reports.Atendimento.DataAccess.DTO;
using Qtech.Reports.Atendimento.Services;
using CeA.Reports.Atendimento.DataAccess.Criteria;

namespace CeA.Reports.Atendimento.Web.WebControls {
    public partial class PesquisaEspecialista : System.Web.UI.UserControl {

        #region -- Event Model ----------------------------------------------------

        // Event Keys
        private static readonly object ConfirmarEventKey = new object();


        // Delegates
        public delegate void ConfirmarEventHandler(object sender, PesquisaEspecialistaEventArgs e);


        // Events
        public event ConfirmarEventHandler Confirmar {
            add {
                Events.AddHandler(ConfirmarEventKey, value);
            }
            remove {
                Events.RemoveHandler(ConfirmarEventKey, value);
            }
        }


        protected virtual void OnConfirmar(PesquisaEspecialistaEventArgs e) {
            ConfirmarEventHandler handler = Events[ConfirmarEventKey] as ConfirmarEventHandler;
            if (handler != null) {
                handler(this, e);
            }
        }
        #endregion


        protected void Page_Init(object sender, EventArgs e)            {
            cmdConfirmar.Click += new EventHandler(cmdConfirmar_Click);
            cmdCancelar.Click += new EventHandler(cmdCancelar_Click);
        }


        protected void Page_Load(object sender, EventArgs e)            {
            if (!Page.IsPostBack) {
                popup.Visible = false;
            }
        }


        protected void cmdPesquisar_Click(object sender, EventArgs e)   {
            EspecialistaCriteria filter = new EspecialistaCriteria();
            filter.Sigla = txtSigla.Text;
            filter.Nome = txtNome.Text;

            List<EspecialistaDTO> ds = CadastroFacade.FindEspecialista(filter);

            dataList.DataSource = ds;
            dataList.DataBind();
        }


        protected void cmdConfirmar_Click(object sender, EventArgs e)   {
            PesquisaEspecialistaEventArgs args = null;

            foreach (Control ctl in dataList.Items) {
                Control c = ctl.FindControl("rbtItem");
                if (c is GroupRadioButton) {
                    GroupRadioButton grb = (c as GroupRadioButton);
                    if (grb.Checked) {
                        Label lblSigla = (c.FindControl("lblSigla") as Label);
                        Label lblNome  = (c.FindControl("lblNome") as Label);

                        args = new PesquisaEspecialistaEventArgs();
                        args.Id = Convert.ToInt32(grb.Text);
                        args.Sigla = lblSigla.Text;
                        args.Nome = lblNome.Text;

                        break;
                    }
                }
            }

            popup.Visible = false;

            if(args != null) OnConfirmar(args);
        }


        protected void cmdCancelar_Click(object sender, EventArgs e)    {
            popup.Visible = false;
        }


        public void pop()                                               {
            popup.Visible = true;
        }
    }
}